package com.wwwarehouse.taskcenter.fragment.job_point.step3;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.button.StateButton;
import com.wwwarehouse.common.custom_widget.switchbutton.SwitchCheckBox;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;
import com.wwwarehouse.taskcenter.eventbus_event.TaskPointCardEvent;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * @创建者 chenchao.
 * @创建时间 2017/10/25 16:53
 * @描述 ${TODO}
 */

public class ChooseConnectPersonFragment3 extends BaseFragment implements View.OnClickListener {

    private View mView;
    private SwitchCheckBox mTaskAddpointStep3Containcheck;
    private SwitchCheckBox mTaskAddpointStep3Manypeopelcheck;
    private StateButton mBtSave;
    private int allowBinding = 1;
    private int allowMore = 1;
    private String jobPointUkid;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_choose_connect_person, container, false);
        initView(mView);
        return mView;
    }

    private void initView(View mView) {
        mTaskAddpointStep3Containcheck = (SwitchCheckBox) mView.findViewById(R.id.task_addpoint_step3_containcheck);
        mTaskAddpointStep3Manypeopelcheck = (SwitchCheckBox) mView.findViewById(R.id.task_addpoint_step3_manypeopelcheck);
        mBtSave = (StateButton) mView.findViewById(R.id.bt_save);

        mBtSave.setOnClickListener(this);

        jobPointUkid = getArguments().getString("jobPointUkid");

        mTaskAddpointStep3Containcheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    allowBinding = 1;
                }else{
                    allowBinding = 0;
                }
            }
        });
        mTaskAddpointStep3Manypeopelcheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    allowMore = 1;
                }else{
                    allowMore = 0;
                }
            }
        });


    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ChooseConnectPersonFragment3) {
            mActivity.setTitle("" + getString(R.string.new_task_step3_title));
        }
    }

    @Override
    public void requestDatas() {

    }

    public void saveData(){
        /**
         * allowBinding	是否允许绑定（1/0）	number	@mock=1
         allowMore	是否允许多关联（1/0）	number	@mock=1
         jobPointUkid	作业点ukid	number	@mock=100000110
         */
        Map<String, String> map = new HashMap<>();
        map.put("allowBinding", "" + allowBinding);
        map.put("allowMore", "" + allowMore);
        map.put("jobPointUkid", "" + jobPointUkid);

        showProgressDialog();
        NoHttpUtils.httpPost(TaskCenterConstant.URL_ADD_JOBPOINT_STEP3, map, myResponseListener, 10001);
    }

    NoHttpUtils.OnResponseListener myResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int i) {
            if ("0".equals(commonClass.getCode())) {
                TaskPointCardEvent event = new TaskPointCardEvent();
                event.setFinish3(true);
                EventBus.getDefault().post(event);
                popFragment();
            }else {
                toast("" + commonClass.getMsg());
            }
        }

        @Override
        public void onFailed(String s, int i) {

        }

        @Override
        public void onFinish(int i) {
            dismissProgressDialog();
        }
    };



    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.bt_save) {
            saveData();

        }
    }
}
